<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\agency\controller;


use app\agency\service\NodeService;
use app\common\facade\Arr;
use app\common\services\UserService;
use app\common\services\wechattemplate\UserTemplateService;
use library\Controller;
use library\tools\Data;
use think\Console;
use think\Db;
use think\exception\HttpResponseException;
use app\common\facade\TransItem;
use app\common\services\JobService;
use app\common\services\PublishJobService;
use app\common\services\JobOptionService;

/**
 * 商家管理
 * Class Index
 * @package app\agency\controller
 */
class Business extends AgentBase
{
    public $table = 'User';

    /**
     * 商家概况
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        //获取当前代理商所在城市
        $user=$this->agentUser;
        $data['provinceid']=$user['provinces_id'];
        $data['cityid']=$user['city_id'];

        $result['total_business']=DB::name($this->table)->where($data)->where('type=2')->count();//商户总数
        $result['sucess_business']=DB::name($this->table)->where($data)->where('type=2 and status=2 ')->count();//商户认证成功
        $result['pre_business']=DB::name($this->table)->where($data)->where('status=1   and type=2')->count();
        $result['deny_business']=DB::name($this->table)->where($data)->where('status=3   and type=2')->count();
        $this->assign('result',$result);
        $this->fetch();

    }

     /**
     * 商家列表
     * @auth true
     * @menu true
     * @throws \ReflectionException
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function businessList(){

        $this->title = '商家列表';
        $status = $this->request->param('status', 0);
        if($status!=0){
            $data[]=['status','EQ',$status];
            if($status==1){
                $this->title = '待认证商家';
            }elseif ($status==2) {
                $this->title = '审核通过商家列表';
            }else {
                $this->title = '审核拒绝商家列表';
            }
        }else{
            $data[]=['status','IN',[1,2,3,0]];
        }

        $result=$this->agentUser;
        $data[]=['provinceid','EQ',$result['provinces_id']];
        $data[]=['cityid','EQ',$result['city_id']];
        $data[]=['type','EQ',2]; 

        $query = $this->_query($this->table)
            ->where($data)
            ->like('title')
            ->equal('id,stauts,name,is_subscribe');
        $query->order('id desc')->page();



    }
    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _businessList_page_filter(&$data)
    {

        $this->clist = Db::name('UserExtendBus')->field("user_id,is_recomend,legal_person_name")->select();
        foreach ($data as &$vo) {
            
            foreach ($this->clist as $cate) if ($cate['user_id'] === $vo['id']) $vo['cate'] = $cate;

        }
    }



    /**
     * 商家审核
     * @auth true
     */
    public function auditBusiness(){

        $id = $this->request->param('id', 0);
        $status = $this->request->param('status', 0);
        if($status==2){
            //认证通过
            $this->_save($this->table, ['status' => '2']);
        }else{
            $message=$this->request->param('content');
            if(empty($message)){
                $this->error('请输入拒绝理由');
            }else{
                //认证通过
                $this->_save($this->table, ['status' => '3',"replay"=>$message]);
            }
        }
    }


    /**
     * 审核商家发送模版消息
     * @param $result
     *
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
   protected function _auditBusiness_save_result($result)
   {
       if ($result) {
           $id = $this->request->param('id', 0);
           $userInfo = (new UserService())->userInfo($id, ['extend']);
           $status = $this->request->param('status', 0);
           if($status==2) {
                if($userInfo['is_send']==0){
                    //3： 送发布豆
                    $user_wallet=DB::name('user_wallet')->where("user_id",$userInfo['id'])->find();

                    Data::save(
                        'user_wallet',
                        [
                            'id'                =>$user_wallet['id'],
                            'user_id'           => $userInfo['id'],
                            'publish_bean'      => sysconf('approve_business_bean')+$user_wallet['publish_bean'],
                        ]
                    );
                    //发布豆流水记录
                    if ((sysconf('approve_business_bean') ?: 0)) {
                        Data::save(
                            'user_recharge_log',
                            [
                                'user_id'           => $userInfo['id'],
                                'recharge_money'    => 0,
                                'publish_bean'      => sysconf('approve_business_bean') ?: 0,
                                'recharge_rate'     => 0,
                                'give_publish_bean' => 0,
                                'give_rate'         => 0,
                                'type'              => 1,
                                'kind'              => 8
                            ]
                        );
                    }
                    //更新user表
                    Data::save(
                        'user',
                        [
                            'id'           => $userInfo['id'],
                            'is_send'      => 1,
                        ]
                    );
                }
               (new UserTemplateService())->approveSuccess($userInfo);
           } else {
               $message=$this->request->param('content');
               (new UserTemplateService())->approveFalse($userInfo, $message);
           }

       }
       return $result;
   }




    public function businessDeny(){
        $this->title = '会员认证拒绝';
        $result=$this->request->param();
        $this->assign('result',$result);
        $this->fetch('business_deny');
    }

     /**
    * 查看认证资料
     * @auth true
     *
     */
    public function businessInfo(){
       

        $id=$this->request->param('id');
        $user=DB::name("UserExtendBus")
            ->alias('ue')
            ->where('ue.user_id',$id)
            ->join('User u', 'u.id = ue.user_id')
            ->field('ue.* ,u.email')
            ->find();

        $this->assign('vo',$user);

        $this->fetch('business_info');
    }

      /**
    * 商家推荐
     * @auth true
     *
     */
    public function recomend(){

        $id = $this->request->param('id', 0);
        $status = $this->request->param('status', 0);
        $this->_save("UserExtendBus", ['is_recomend' => $status,'user_id' => $id]);
    }







}
